package com.sgb.zhaocai.view.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.link.entity.PickInvitedApply;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.databinding.PickMarkeUserFragmentBinding;
import com.sgb.zhaocai.databinding.PickMemberUnitFragmentBinding;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;

/**
 * 市场客户
 */
public class PickMarketUserFragment extends AppLazyFragment {
    private PickMarkeUserFragmentBinding mBinding;
    private static String SHOWTWOTAB = "showtwotab";
    private static String SHOWTWOCOMPANYLIST = "ShowTwoCompanyList";
    private static String DATA = "data";
    private ArrayList<String> titles;
    private ArrayList<Fragment> mFragments;
    private boolean showtwotab;
    private boolean showTwoCompanyList;
    private PickInvitedApply bean;
    private static String FROMWHERE = "fromWhere";
    private String fromWhere;
    private String SearchNames = "";
    private int position = 0;
    private EditText texts;

    public static PickMarketUserFragment newInstance(boolean ShowTwoTab, boolean ShowTwoCompanyList, PickInvitedApply pickinvitedapply, String fromWhere) {
        PickMarketUserFragment fragment = new PickMarketUserFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(SHOWTWOTAB, ShowTwoTab);
        bundle.putBoolean(SHOWTWOCOMPANYLIST, ShowTwoCompanyList);
        bundle.putSerializable(DATA,pickinvitedapply);
        bundle.putString(FROMWHERE, fromWhere);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.pick_marke_user_fragment, container, false);
        showtwotab = (boolean) getArguments().get(SHOWTWOTAB);
        showTwoCompanyList = (boolean) getArguments().get(SHOWTWOCOMPANYLIST);
        bean = (PickInvitedApply) getArguments().get(DATA);
        fromWhere =(String) getArguments().get(FROMWHERE);
        if (showtwotab){
            mBinding.tabLayout.setVisibility(View.VISIBLE);
        }else {
            mBinding.tabLayout.setVisibility(View.GONE);
        }
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        titles = new ArrayList<>();
        mFragments = new ArrayList<>();
        if (showTwoCompanyList){
            titles.add("企业");
            mFragments.add(PickMarketCompanyFragment.newInstance(bean,fromWhere));
        }else {
            titles.add("企业");
            titles.add("个人");
            mFragments.add(PickMarketCompanyFragment.newInstance(bean,fromWhere));
            mFragments.add(PickMarketPersonalFragment.newInstance(bean,fromWhere));
        }
        String[] strings = new String[titles.size()];
        String[] mTitles = titles.toArray(strings);
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitles);
        mBinding.viewPager.setOffscreenPageLimit(mTitles.length);
        mBinding.viewPager.setAdapter(mPagerAdapter);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        //不可滑动设置
        mBinding.viewPager.setNoScroll(true);
        updateTabTextView(mBinding.tabLayout.getTabAt(mBinding.tabLayout.getSelectedTabPosition()), true);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                position = tab.getPosition();
                initSearch(SearchNames);
                setHiet(texts);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void onLazyLoad() {

    }

   //搜索
    public void initSearch(String SearchName) {
        SearchNames = SearchName;
        Fragment fragment = mFragments.get(position);
        if (fragment!=null&&fragment instanceof PickMarketCompanyFragment) {
            ((PickMarketCompanyFragment) fragment).initSearch(SearchName);
        }
        if (fragment!=null&&fragment instanceof PickMarketPersonalFragment) {
            ((PickMarketPersonalFragment) fragment).initSearch(SearchName);
        }
    }

    //设置搜索的hint显示
    public void setHiet(EditText text) {
        this.texts = text;
        Fragment fragment = mFragments.get(position);
        if (fragment!=null&&fragment instanceof PickMarketCompanyFragment) {
            if (TextUtils.isEmpty(SearchNames)){
                if (texts ==null)return;
                texts.setHint("请输入企业名称");
            }
        }
        if (fragment!=null&&fragment instanceof PickMarketPersonalFragment) {
            if (TextUtils.isEmpty(SearchNames)){
                if (texts ==null)return;
                texts.setHint("请输入姓名");
            }
        }
    }

    @Override
    public void onVisible() {
        super.onVisible();
        mBinding.tabLayout.getTabAt(0).select();
        RemoveScreenData();
    }

    /**
     * 会员单位和市场单位切换时清除筛选项数据
     */
    public void RemoveScreenData() {
        for (Fragment fragment : mFragments) {
            if (fragment!=null&&fragment instanceof PickMarketCompanyFragment) {
                ((PickMarketCompanyFragment) fragment).RemoveScreenData();
            }
        }
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) return;
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(getResources().getColor(R.color.color_main));
            tabSelect.setTextSize(14);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(getResources().getColor(R.color.color_tab_one));
            tabUnSelect.setTextSize(14);
            tabUnSelect.setText(tab.getText());
        }
    }
}
